﻿#region Copyright © 2008 by Christian Rodemeyer
// $HeadURL: http://rodemeyer.googlecode.com/svn/trunk/Rodemeyer/WPF/ScreenHelper.cs $
// $Revision: 26 $
#endregion

using System;
using System.Windows;

using Screen = System.Windows.Forms.Screen;
using DrawingRectangle = System.Drawing.Rectangle;
using DrawingPoint = System.Drawing.Point;

namespace Rodemeyer.Wpf
{
    public static class ScreenHelper
    {
        public static System.Drawing.Rectangle MoveRectangleIntoWorkingArea(System.Drawing.Rectangle r)
        {
            System.Drawing.Point center = new System.Drawing.Point(r.Left + r.Width / 2, r.Top + r.Height / 2);
            System.Drawing.Rectangle workarea = Screen.GetWorkingArea(center);
            if (r.Right > workarea.Right) r.X = workarea.Right - r.Width;
            if (r.X < workarea.Left) r.X = workarea.Left;
            if (r.Bottom > workarea.Bottom) r.Y = workarea.Bottom - r.Height;
            if (r.Top < workarea.Top) r.Y = workarea.Top;

            return r;
        }

        public static Rect MoveRectIntoWorkingArea(Rect r)
        {
            return RectFromRectangle(MoveRectangleIntoWorkingArea(RectangleFromRect(r)));
        }

        public static Rect GetWorkingArea(Window w)
        {
            return GetWorkingArea(WpfHelper.GetWindowBounds(w));
        }

        public static Rect GetWorkingArea(Rect r)
        {
            return RectFromRectangle(Screen.GetWorkingArea(RectangleFromRect(r)));
        }

        public static Rect GetWorkingArea(System.Windows.Point p)
        {
            return RectFromRectangle(Screen.GetWorkingArea(new System.Drawing.Point((int)p.X, (int)p.Y)));
        }

        public static System.Drawing.Rectangle RectangleFromRect(Rect r)
        {
            return new System.Drawing.Rectangle((int)r.X, (int)r.Y, (int)r.Width, (int)r.Height);
        }

        public static Rect RectFromRectangle(System.Drawing.Rectangle r)
        {
            return new Rect(r.X, r.Y, r.Width, r.Height);
        }

    }
}
